package cn.sinata.wanglin.property.activity;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.xilada.xldutils.activitys.TitleActivity;
import com.xilada.xldutils.adapter.BaseRecyclerAdapter;
import com.xilada.xldutils.adapter.util.ViewHolder;
import com.xilada.xldutils.view.TwoTextLinearView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.sinata.wanglin.R;
import cn.sinata.wanglin.model.Image;
import cn.sinata.wanglin.model.Myrepair;
import cn.sinata.wanglin.model.RepairDetail;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;
import cn.sinata.wanglin.utils.ImageUtil;
import cn.sinata.wanglin.utils.RelativeDateFormat;
import me.nereo.multi_image_selector.adapter.ImageGridAdapter;

public class RepairRecordsDetailActivity extends TitleActivity {

    private String id;
    @BindView(R.id.ttlv_apply_time)
    TwoTextLinearView ttlv_apply_time;
    @BindView(R.id.ttlv_apply_project)
    TwoTextLinearView ttlv_apply_project;
    @BindView(R.id.ttlv_booking_time)
    TwoTextLinearView ttlv_booking_time;
    @BindView(R.id.tv_content)
    TextView tv_content;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    private ImageAdapter imageAdapter;
    private ArrayList<Image> images = new ArrayList<>();

    @Override
    protected int setContentId() {
        return R.layout.activity_repair_records_detail;
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("报修详情");

        id = getIntent().getStringExtra("id");

        mRecyclerView.setLayoutManager(new GridLayoutManager(this,4));
        mRecyclerView.setHasFixedSize(true);
        imageAdapter = new ImageAdapter(images);
        mRecyclerView.setAdapter(imageAdapter);
        showDialog();
        getData();
    }

    void getData(){
        HttpManager.getRepairDetail(id).subscribe(new ResultDataSubscriber<RepairDetail>(this) {
            @Override
            public void onSuccess(String msg, RepairDetail repairDetail) {
                showToast(msg);
                Myrepair myrepair = repairDetail.getRepair();
                ttlv_apply_time.setRightText(RelativeDateFormat.getTimeYMDHMS(myrepair.getCreateDate()));
                ttlv_apply_project.setRightText(myrepair.getProject());
                ttlv_booking_time.setRightText(RelativeDateFormat.getTimeYMDHMS(myrepair.getBespokeDate()));
                tv_content.setText(myrepair.getRemark());
                images.clear();
                if (repairDetail.getImages() != null && repairDetail.getImages().size() > 0) {
                    images.addAll(repairDetail.getImages());
                    mRecyclerView.setVisibility(View.VISIBLE);
                } else {
                    mRecyclerView.setVisibility(View.GONE);
                }
                imageAdapter.notifyDataSetChanged();
            }
        });
    }

    private class ImageAdapter extends BaseRecyclerAdapter<Image> {

        public ImageAdapter(List<Image> mData) {
            super(mData, R.layout.item_image_grid);
        }

        @Override
        public void onBind(int position, Image image, ViewHolder holder) {
            SimpleDraweeView simpleDraweeView = holder.bind(R.id.image);
            simpleDraweeView.setImageURI(ImageUtil.parseImageUri(image.getUrl()));
        }
    }

}
